package com.authManager.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotEmpty;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authManager.entity.SysRoleMenu;
import com.authManager.server.service.ISysRoleMenuService;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 【角色菜单关联表】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-16
 */

@Api(value = "/admin/sysRoleMenu", tags = "【角色菜单关联表】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysRoleMenu", produces = BaseController.defProduces)
public class SysRoleMenuController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysRoleMenuController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysRoleMenuService sysRoleMenuService;

	@ApiOperation(value = "添加【角色菜单关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "系统code", required = true), })
	@RequestMapping(value = "saveSysRoleMenu", method = { RequestMethod.POST })
	public ResultData<Object> saveSysRoleMenu(@RequestParam(required = true) Long sysRoleId,
			@RequestParam(required = false) String sysMenuIds,
			@RequestParam(required = true) @NotEmpty(message = "参数[systemCode]不能为空") String systemCode) {
		return sysRoleMenuService.giveRoleMenu(sysRoleId, systemCode, sysMenuIds, getCurrUserLoginId());
	}

	@ApiOperation(value = "修改【角色菜单关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleMenuIdStr", value = "主键id", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "系统code", required = true), })
	@RequestMapping(value = "editSysRoleMenu", method = { RequestMethod.POST })
	public ResultData<Object> editSysRoleMenu(@RequestParam(required = true) Long sysRoleMenuId,
			@RequestParam(required = true) Long sysRoleId, @RequestParam(required = false) Long sysMenuId,
			@RequestParam(required = true) @NotEmpty(message = "参数[systemCode]不能为空") String systemCode) {
		SysRoleMenu sysRoleMenu = sysRoleMenuService.selectById(sysRoleMenuId);
		if (sysRoleMenu == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysRoleMenu = new SysRoleMenu();
		if (sysRoleMenuId != null) {
			sysRoleMenu.setSysRoleMenuId(sysRoleMenuId);
		}
		if (sysRoleId != null) {
			sysRoleMenu.setSysRoleId(sysRoleId);
		}
		if (sysMenuId != null) {
			sysRoleMenu.setSysMenuId(sysMenuId);
		}
		if (StringUtils.isNotBlank(systemCode)) {
			sysRoleMenu.setSystemCode(systemCode);
		}
		// sysRoleMenu.setCreater(null);
		// sysRoleMenu.setCreateTime(null);
		sysRoleMenuService.updateById(sysRoleMenu);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【角色菜单关联表】")
	@RequestMapping(value = "delSysRoleMenu", method = { RequestMethod.POST })
	public ResultData<Object> delSysRoleMenu(@RequestParam(required = true) Long sysRoleMenuId) {
		SysRoleMenu sysRoleMenu = sysRoleMenuService.selectById(sysRoleMenuId);
		if (sysRoleMenu == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		// sysRoleMenuService.deleteById(sysRoleMenuId);
		// 逻辑删除
		// sysRoleMenu.setStatus(0);
		// sysRoleMenuService.updateById(sysRoleMenuId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【角色菜单关联表】")
	@RequestMapping(value = "getSysRoleMenu", method = { RequestMethod.POST })
	public ResultData<SysRoleMenu> getSysRoleMenu(@RequestParam(required = true) Long sysRoleMenuId) {
		SysRoleMenu sysRoleMenu = sysRoleMenuService.selectById(sysRoleMenuId);
		if (sysRoleMenu == null) {
			return new ResultData.Builder<SysRoleMenu>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysRoleMenu>(ResultCode.success).setData(sysRoleMenu).build();
	}

	@ApiOperation(value = "列表【角色菜单关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleMenuId", value = "主键id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "系统code", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "start_createTime", value = "开始-创建时间", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "end_createTime", value = "截至-创建时间", required = false), })
	@RequestMapping(value = "listSysRoleMenu", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysRoleMenu>> listSysRoleMenu(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysRoleMenuId, @RequestParam(required = false) Long sysRoleId,
			@RequestParam(required = false) Long sysMenuId, @RequestParam(required = false) String systemCode,
			@RequestParam(required = false) String start_createTime,
			@RequestParam(required = false) String end_createTime) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键id
		if (sysRoleMenuId != null) {
			paramMap.put("sysRoleMenuId", sysRoleMenuId);
		}
		// 角色id
		if (sysRoleId != null) {
			paramMap.put("sysRoleId", sysRoleId);
		}
		// 资源id
		if (sysMenuId != null) {
			paramMap.put("sysMenuId", sysMenuId);
		}
		// 系统code
		if (StringUtils.isNotBlank(systemCode)) {
			paramMap.put("systemCode", systemCode);
		}
		// 开始-创建时间
		if (StringUtils.isNotBlank(start_createTime)) {
			paramMap.put("start_createTime", start_createTime);
		}
		// 结束-创建时间
		if (StringUtils.isNotBlank(end_createTime)) {
			paramMap.put("end_createTime", end_createTime);
		}
		return sysRoleMenuService.listSysRoleMenu(paramMap);
	}
}
